<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Controller\AdminController;

class StatisticsController extends AdminController {
    
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'statistics'); // 数据集
    }
    
    public function index(){
        
        
        $rea = getViewJson(30,'day','m-d');
        $this->assign('viewsArray',$rea);
        
        $res = getUserJson(30,'day','m-d');
        $this->assign('usersArray',$res);
        
        $re = getJson(30,'day','m-d');
        $this->assign('chartArray',$re);
        
        $this->assign('action', 'index');
        $this->display();
    }
    
    public function  users(){
        
        $order = I('type');
        if(empty($order)){
            $order="sale";
        }
        switch ($order) {
            case 'sale':
                $orders = "user_orders desc"; 
                break;
            case 'points':
                $orders = "user_points desc"; 
                break;
            case 'nums':
                $orders = "user_login_num desc"; 
                break;
            default:
                $orders = "user_orders desc";
        }
        $this->assign('order', $order);
        
        $p = I('p');
        if($p ==1){
            $p =0;
        }
        
        $where['user_orders'] = array('gt',0);
        $good =M('users')->where($where)->order($orders)->select();// 查询满足要求的总记录数
        $count = count($good);
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出t
        $goods = M('users')->where($where)->order($orders)->limit($Page->firstRow.','.$Page->listRows)->select();
        
        $this->assign('p', $p);
        $this->assign('pages', $show);
        $this->assign('goods', $goods);
        
        $this->assign('action', 'users');
        $this->display();
    }

    







    //商品销量排行
    public function goods(){
        $p = I('p');
        if($p ==1){
            $p =0;
        }
        $where['goods_realsale'] = array('gt',0);
        $good =M('goods')->where($where)->order('goods_realsale desc')->select();// 查询满足要求的总记录数
        $count = count($good);
        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出t
        $goods = M('goods')->where($where)->order('goods_realsale desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        
        $this->assign('p', $p);
        $this->assign('pages', $show);
        $this->assign('goods', $goods);
        $this->assign('action', 'goods');
        $this->display();
    }
    
    //商品的销量详情
    public function saleList(){
        
        $p = I('p');
        if($p ==1){
            $p =0;
        }
        
        $where['oi.goods_id'] = I('goods_id');
        $where['os.order_state'] =  array('EGT',20);
        //$count = M('order_goods')->where($where)->count();// 查询满足要求的总记录数
        $Order = M("order_goods");
        $count = $Order->alias('oi')->join('LEFT JOIN __ORDER__ os ON oi.order_id = os.order_id')->where($where)->count();

        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出t
        $goods = $Order->alias('oi')->join('LEFT JOIN __ORDER__ os ON oi.order_id = os.order_id')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        
        foreach ($goods as $key => $value) {
            $map['order_id'] = $value['order_id'];
            $address[$key] = unserialize(M('order_common')->where($map)->getField('reciver_info'));
            $goods[$key] = array_merge($goods[$key],$address[$key]);
            $goods[$key]['totalPrice'] = price($goods[$key]['goods_price']*$goods[$key]['goods_realsale']);
        }
        //排序
        $this->assign('p', $p);
        $this->assign('pages', $show);
        $this->assign('goods', $goods);
        $this->assign('action', 'goods');
        $this->display();
    }
    
    //销量前100
    public function saleCost(){
        
        $where['goods_realsale'] = array('gt',0);
        $goods =M('goods')->field('goods_id,goods_title,goods_thumb,goods_price,goods_realsale')->where($where)->select();// 查询满足要求的总记录数
        foreach ($goods as $key => $value) {
            $goods[$key]['totalPrice'] = price($value['goods_price']*$value['goods_realsale']);
        }
        multi_array_sort($goods, "totalPrice");
        $this->assign('goods', $goods);
        $this->assign('action', 'goods');
        $this->display();
    }

}
